<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Help Desk
 * @version 	1.0
 * @author 		Kurosaki <kurosaki@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

define('PS_ADMIN_DIR', getcwd());
include(PS_ADMIN_DIR.'/../../config/config.inc.php');
/* Getting cookie or logout */
require_once(dirname(__FILE__).'/../../init.php');
/**
* when change position for status
*/
if (array_key_exists('ajaxStatusPositions', $_POST))
{
	$id_status_to_move = (int)(Tools::getValue('id_status_to_move'));
	$id_status_parent = (int)(Tools::getValue('id_status_parent'));		// !important
	$way = (int)(Tools::getValue('way'));
	$positions = Tools::getValue('zhelpdesk_ticket_status');
	if (is_array($positions))
		foreach ($positions AS $key => $value) {
			$pos = explode('_', $value);
			if ((isset($pos[1]) AND isset($pos[2])) AND ($pos[1] == $id_status_parent AND $pos[2] == $id_status_to_move)) {
				$position = $key;
				break;
			}
		}
	include_once('classes/Zstatus.php');
	$tabclass = new Zstatus($id_status_to_move);
	if (Validate::isLoadedObject($tabclass))
	{
		if (isset($position) && $tabclass->updatePosition($way, $position)) {
			die(true);
		} else
			die('{"hasError" : true, errors : "Can not update tabs position"}');
	} else
		die('{"hasError" : true, "errors" : "This tab can not be loaded"}');
}
/**
* when change position for priority
*/
if (array_key_exists('ajaxPriorityPositions', $_POST))
{
	$id_priority_to_move = (int)(Tools::getValue('id_priority_to_move'));
	$id_priority_parent = (int)(Tools::getValue('id_priority_parent'));		// !important
	$way = (int)(Tools::getValue('way'));
	$positions = Tools::getValue('zhelpdesk_ticket_priority');
	if (is_array($positions))
		foreach ($positions AS $key => $value) {
			$pos = explode('_', $value);
			if ((isset($pos[1]) AND isset($pos[2])) AND ($pos[1] == $id_priority_parent AND $pos[2] == $id_priority_to_move)) {
				$position = $key;
				break;
			}
		}
	include_once('classes/Zpriority.php');
	$tabclass = new Zpriority($id_priority_to_move);
	
	if (Validate::isLoadedObject($tabclass))
	{
		if (isset($position) && $tabclass->updatePosition($way, $position)) {
			die(true);
		} else
			die('{"hasError" : true, errors : "Can not update tabs position"}');
	} else
		die('{"hasError" : true, "errors" : "This tab can not be loaded"}');
}
/**
* when change position for rating
*/
if (array_key_exists('ajaxRatingPositions', $_POST))
{
	$id_rating_to_move = (int)(Tools::getValue('id_rating_to_move'));
	$id_rating_parent = (int)(Tools::getValue('id_rating_parent'));		// !important
	$way = (int)(Tools::getValue('way'));
	$positions = Tools::getValue('zhelpdesk_ticket_rating');
	if (is_array($positions))
		foreach ($positions AS $key => $value) {
			$pos = explode('_', $value);
			if ((isset($pos[1]) AND isset($pos[2])) AND ($pos[1] == $id_rating_parent AND $pos[2] == $id_rating_to_move)) {
				$position = $key;
				break;
			}
		}
	include_once('classes/Zrating.php');
	$tabclass = new Zrating($id_rating_to_move);
	if (Validate::isLoadedObject($tabclass))
	{
		if (isset($position) && $tabclass->updatePosition($way, $position)) {
			die(true);
		} else
			die('{"hasError" : true, errors : "Can not update tabs position"}');
	} else
		die('{"hasError" : true, "errors" : "This tab can not be loaded"}');
}